//
//  LadingGoodCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/3.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "LadingGoodCell.h"
#import "LadingGoodMainModel.h"

@interface LadingGoodCell ()

@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) UILabel *noTitleLab;
@property (nonatomic, strong) UILabel *noLab;
@property (nonatomic, strong) UILabel *nameTitleLab;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UILabel *packingTitleLab;
@property (nonatomic, strong) UILabel *packingLab;
@property (nonatomic, strong) UILabel *weightTitleLab;
@property (nonatomic, strong) UILabel *weightLab;
@property (nonatomic, strong) UILabel *takeTitleLab;
@property (nonatomic, strong) UILabel *takeLab;
@property (nonatomic, strong) UILabel *volumeTitleLab;
@property (nonatomic, strong) UILabel *volumeLab;
@property (nonatomic, strong) UITextField *countField;
@property (nonatomic, strong) UITextField *pieceField;
@property (nonatomic, strong) UILabel *line;
@property (nonatomic, strong) UIButton *openBtn;

@end

@implementation LadingGoodCell

+ (instancetype)LadingGoodCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"LadingGoodCell";
    LadingGoodCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[LadingGoodCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = HexColor(0xffffff);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.selectBtn = [UIButton new];
        [self.selectBtn addTarget:self action:@selector(selectClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.selectBtn];
        
        self.noTitleLab = [UILabel new];
        self.noTitleLab.textColor = HexColor(0x585974);
        self.noTitleLab.font = [UIFont systemFontOfSize:14];
        self.noTitleLab.text = @"货物编号：";
        [self.contentView addSubview:self.noTitleLab];
        
        self.noLab = [UILabel new];
        self.noLab.textColor = HexColor(0x3D68EB);
        self.noLab.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.noLab];
        
        self.nameTitleLab = [UILabel new];
        self.nameTitleLab.textColor = HexColor(0x585974);
        self.nameTitleLab.font = [UIFont systemFontOfSize:14];
        self.nameTitleLab.text = @"货物名称：";
        [self.contentView addSubview:self.nameTitleLab];
        
        self.nameLab = [UILabel new];
        self.nameLab.textColor = HexColor(0x2B2C44);
        self.nameLab.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.nameLab];
        
        self.openBtn = [UIButton new];
        [self.openBtn addTarget:self action:@selector(openClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.openBtn];
        
        self.packingTitleLab = [UILabel new];
        self.packingTitleLab.textColor = HexColor(0x585974);
        self.packingTitleLab.font = [UIFont systemFontOfSize:14];
        self.packingTitleLab.text = @"包装名称：";
        [self.contentView addSubview:self.packingTitleLab];
        
        self.packingLab = [UILabel new];
        self.packingLab.textColor = HexColor(0x2B2C44);
        self.packingLab.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.packingLab];
        
        self.weightTitleLab = [UILabel new];
        self.weightTitleLab.textColor = HexColor(0x585974);
        self.weightTitleLab.font = [UIFont systemFontOfSize:14];
        self.weightTitleLab.text = @"重量(kg)：";
        [self.contentView addSubview:self.weightTitleLab];
        
        self.weightLab = [UILabel new];
        self.weightLab.textColor = HexColor(0x2B2C44);
        self.weightLab.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.weightLab];
        
        self.takeTitleLab = [UILabel new];
        self.takeTitleLab.textColor = HexColor(0x585974);
        self.takeTitleLab.font = [UIFont systemFontOfSize:14];
        self.takeTitleLab.text = @"收容数：";
        [self.contentView addSubview:self.takeTitleLab];
        
        self.takeLab = [UILabel new];
        self.takeLab.textColor = HexColor(0x2B2C44);
        self.takeLab.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.takeLab];
        
        self.volumeTitleLab = [UILabel new];
        self.volumeTitleLab.textColor = HexColor(0x585974);
        self.volumeTitleLab.font = [UIFont systemFontOfSize:14];
        self.volumeTitleLab.text = @"体积(m³)：";
        [self.contentView addSubview:self.volumeTitleLab];
        
        self.volumeLab = [UILabel new];
        self.volumeLab.textColor = HexColor(0x2B2C44);
        self.volumeLab.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.volumeLab];
        
        self.countField = [UITextField new];
        self.countField.placeholder = @"请输入货物数量";
        self.countField.textColor = HexColor(0x2B2C44);
        self.countField.font = [UIFont systemFontOfSize:14];
        self.countField.backgroundColor = HexColor(0xF7F7FA);
        self.countField.layer.masksToBounds = YES;
        self.countField.layer.cornerRadius = 4;
        self.countField.layer.borderWidth = 1;
        self.countField.layer.borderColor = HexColor(0xF2F2F5).CGColor;
        self.countField.keyboardType = UIKeyboardTypeNumberPad;
        [self.countField addTarget:self action:@selector(countFieldChange:) forControlEvents:UIControlEventEditingDidEnd];
        [self.contentView addSubview:self.countField];
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 32)];
        leftView.backgroundColor = HexColor(0xF7F7FA);
        self.countField.leftView = leftView;
        self.countField.leftViewMode = UITextFieldViewModeAlways;
        
        self.pieceField = [UITextField new];
        self.pieceField.placeholder = @"件数";
        self.pieceField.textColor = HexColor(0x2B2C44);
        self.pieceField.font = [UIFont systemFontOfSize:14];
        self.pieceField.backgroundColor = HexColor(0xF7F7FA);
        self.pieceField.layer.masksToBounds = YES;
        self.pieceField.layer.cornerRadius = 4;
        self.pieceField.layer.borderWidth = 1;
        self.pieceField.layer.borderColor = HexColor(0xF2F2F5).CGColor;
        self.pieceField.keyboardType = UIKeyboardTypeNumberPad;
        [self.pieceField addTarget:self action:@selector(pieceFieldChange:) forControlEvents:UIControlEventEditingDidEnd];
        [self.contentView addSubview:self.pieceField];
        UIView *leftView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 32)];
        leftView2.backgroundColor = HexColor(0xF7F7FA);
        self.pieceField.leftView = leftView2;
        self.pieceField.leftViewMode = UITextFieldViewModeAlways;
        
        self.line = [UILabel new];
        self.line.backgroundColor = HexColor(0xF2F2F5);
        [self.contentView addSubview:self.line];
    }
    return self;
}

- (void)countFieldChange:(UITextField *)field {
    if (self.countBlock) {
        self.countBlock(field.text);
    }
}

- (void)pieceFieldChange:(UITextField *)field {
    if (self.pieceBlock) {
        self.pieceBlock(field.text);
    }
}

- (void)selectClick {
    if (self.selectBlock) {
        self.selectBlock();
    }
}

- (void)openClick {
    if (self.openBlock) {
        self.openBlock();
    }
}

-(void)setModel:(LadingGoodMainModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    CGFloat cellW = ScreenW - 30;
    self.selectBtn.frame = CGRectMake(12, 29, 23, 23);
    [self.selectBtn setImage:[UIImage imageNamed:self.model.type==1?@"icon_select_y":@"icon_select_n"] forState:0];
    self.openBtn.frame = CGRectMake(cellW - 15 - 18, 16, 18, 18);
    [self.openBtn setImage:[UIImage imageNamed:self.model.isOpen?@"icon_arrow_up":@"icon_arrow_down"] forState:0];
    self.noTitleLab.nim_left = self.selectBtn.nim_right + 10;
    self.noTitleLab.nim_top = self.selectBtn.nim_top - 14;
    [self.noTitleLab sizeToFit];
    
    self.noLab.nim_left = self.noTitleLab.nim_right;
    self.noLab.nim_top = self.noTitleLab.nim_top;
    self.noLab.text = self.model.num;
    [self.noLab sizeToFit];
    self.noLab.nim_width = self.openBtn.nim_left - 10 - self.noLab.nim_left;
    
    self.nameTitleLab.nim_left = self.noTitleLab.nim_left;
    self.nameTitleLab.nim_top = self.selectBtn.nim_top + 16;
    [self.nameTitleLab sizeToFit];
    
    self.nameLab.nim_left = self.nameTitleLab.nim_right;
    self.nameLab.nim_top = self.nameTitleLab.nim_top;
    self.nameLab.text = self.model.name;
    [self.nameLab sizeToFit];
    self.nameLab.nim_width = self.openBtn.nim_left - 10 - self.nameLab.nim_left;
    
    CGFloat fieldTop = 0;
    if (self.model.isOpen) {
        self.packingTitleLab.nim_left = self.noTitleLab.nim_left;
        self.packingTitleLab.nim_top = self.nameTitleLab.nim_bottom + 10;
        [self.packingTitleLab sizeToFit];
        
        CGFloat midW = (self.openBtn.nim_left - self.noTitleLab.nim_left - 40) * 0.5;
        self.packingLab.text = self.model.packType==1?@"围板箱":@"周转箱";
        self.packingLab.nim_left = self.packingTitleLab.nim_right;
        [self.packingLab sizeToFit];
        self.packingLab.nim_width = midW - self.packingTitleLab.nim_width + 30;
        self.packingLab.nim_centerY = self.packingTitleLab.nim_centerY;
        
        self.weightTitleLab.nim_left = self.packingLab.nim_right + 10;
        [self.weightTitleLab sizeToFit];
        self.weightTitleLab.nim_centerY = self.packingLab.nim_centerY;
        
        self.weightLab.text = [NSString stringWithFormat:@"%@",self.model.weight];
        self.weightLab.nim_left = self.weightTitleLab.nim_right;
        [self.weightLab sizeToFit];
        self.weightLab.nim_width = midW - self.weightTitleLab.nim_width;
        self.weightLab.nim_centerY = self.weightTitleLab.nim_centerY;
        
        self.takeTitleLab.nim_left = self.noTitleLab.nim_left;
        self.takeTitleLab.nim_top = self.packingTitleLab.nim_bottom + 10;
        [self.takeTitleLab sizeToFit];
        
        self.takeLab.text = [NSString stringWithFormat:@"%d",self.model.takeNum];
        self.takeLab.nim_left = self.takeTitleLab.nim_right;
        [self.takeLab sizeToFit];
        self.takeLab.nim_width = midW - self.takeTitleLab.nim_width + 30;
        self.takeLab.nim_centerY = self.takeTitleLab.nim_centerY;
        
        self.volumeTitleLab.nim_left = self.takeLab.nim_right + 10;
        [self.volumeTitleLab sizeToFit];
        self.volumeTitleLab.nim_centerY = self.takeLab.nim_centerY;
        
        self.volumeLab.nim_left = self.volumeTitleLab.nim_right;
        self.volumeLab.text = [NSString stringWithFormat:@"%@",self.model.volume];
        [self.volumeLab sizeToFit];
        self.volumeLab.nim_width = midW - self.volumeTitleLab.nim_width;
        self.volumeLab.nim_centerY = self.volumeTitleLab.nim_centerY;
        
        fieldTop = self.takeTitleLab.nim_bottom + 10;
    }else {
        self.packingTitleLab.frame = CGRectZero;
        self.packingLab.frame = CGRectZero;
        self.weightTitleLab.frame = CGRectZero;
        self.weightLab.frame = CGRectZero;
        self.takeTitleLab.frame = CGRectZero;
        self.takeLab.frame = CGRectZero;
        self.volumeTitleLab.frame = CGRectZero;
        self.volumeLab.frame = CGRectZero;
        fieldTop = self.nameTitleLab.nim_bottom + 10;
    }
    
    CGFloat fieldW = (cellW - 15 - 5 - self.noTitleLab.nim_left) * 0.5;
    
    self.countField.frame = CGRectMake(self.noTitleLab.nim_left, fieldTop, fieldW, 32);
    self.countField.text = self.model.default_num?[NSString stringWithFormat:@"%d",self.model.default_num]:@"";
    self.pieceField.frame = CGRectMake(self.countField.nim_right + 10, fieldTop, fieldW, 32);
    self.pieceField.text = self.model.default_pnum?[NSString stringWithFormat:@"%d",self.model.default_pnum]:@"";
    self.line.frame = CGRectMake(10, self.countField.nim_bottom + 15, cellW - 20, 1);
    
    self.height = self.line.nim_bottom;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
